const { set } = require("mobx-miniprogram");

// pages/AllPlaylist/AllPlaylist.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hotPlaylist:[],
        tagsPosition:100,
        playlistCurrent:0,
        playlist:[],
        pageHeight:110*15 ,
        p:[],
        offset:1,
        isshow:false,
    },
    // 获取热门歌单
    async getPlaylistHot(){
        const res = await wx.p.request({
            url:`${wx.root}/playlist/hot`
        })
        res.data.tags.forEach(item => {
            item.choose = false
        });
        res.data.tags[0].choose = true
        this.setData({
            hotPlaylist:res.data.tags,
            toView:'item'+res.data.tags[5].id
        })
        let query = wx.createSelectorQuery()
        for (let i = 0; i < res.data.tags.length; i++) {
            query.select(`#item${i}`).boundingClientRect(ress => {
                if(i == 0) res.data.tags[i].left = 0
                else res.data.tags[i].left = res.data.tags[i-1].left+ress.width
            }).exec()
        }    
        this.setData({
            hotPlaylist:res.data.tags
        })
    },
    // 清除所有选择
    removeChooseTags(){
        const res = this.data.hotPlaylist
        res.forEach(item=>{
            item.choose = false
        })
        this.setData({
            hotPlaylist:res
        })
    },
    // 改变选择
    changeTags(e){
        let index = e.currentTarget.dataset.index
        this.setData({
            playlistCurrent:index
        })
    },
    // 改变歌单
    changePlaylist(e){
        wx.pageScrollTo({
            scrollTop:0,
            duration:0
        })
        let index = e.detail.current
        this.setData({
            tagsPosition:this.data.hotPlaylist[index].left-132,
            playlistCurrent:index,
            pageHeight:110*15,
        })
    },
    // 获取歌单
    async getPlaylist(index){
        let item = this.data.hotPlaylist[index]
        const res = await wx.p.request({
            url:`${wx.root}/top/playlist`,
            data:{  
                cat:item.name,
                limit:15,
                offset:0
            }
        })
        for (let index = 0; index < res.data.playlists.length; index++) {
            const item = res.data.playlists[index];
            if(item.playCount >= 100000000) 
                item.count = parseInt(item.playCount/100000000)+'亿'
            else if(item.playCount >= 10000)
                item.count = parseInt(item.playCount/10000)+'万'
            else item.count = item.playCount+''
        }
        this.data.p.push(res.data.playlists)
    },
    // 获取单个歌单
    async  getOnePlaylist(){
        this.setData({
            isshow:true
        })
        wx.showLoading({
            title: 'loading',
          })
          const item = this.data.hotPlaylist[this.data.playlistCurrent]
          const res = await wx.p.request({
              url:`${wx.root}/top/playlist`,
              data:{  
                  cat:item.name,
                  limit:30,
                  offset:this.data.offset*30
              }
          })
          for (let index = 0; index < res.data.playlists.length; index++) {
            const item = res.data.playlists[index];
            if(item.playCount >= 100000000) 
                item.count = parseInt(item.playCount/100000000)+'亿'
            else if(item.playCount >= 10000)
                item.count = parseInt(item.playCount/10000)+'万'
            else item.count = item.playCount+''
        }
          wx.hideLoading({
            success: () => {
                this.setData({
                    pageHeight:this.data.pageHeight+3300,
                    [ `playlist[${this.data.playlistCurrent}]`]:[...this.data.playlist[`${this.data.playlistCurrent}`],...res.data.playlists],
                    offset:this.data.offset+1,
                    isshow:false
                })
            },
          })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.showLoading({
          title: '加载中',
        })
        this.getPlaylistHot()
        setTimeout(() => {
            for (let i = 0; i < 10; i++) {
                this.getPlaylist(i)
            }   
            setTimeout(()=>{
                this.setData({
                    playlist:this.data.p
                })
                wx.hideLoading({
                  success: (res) => {},
                })
            },1000)
        }, 100);
        
    },
    onReachBottom: function () {
        if(this.data.isshow == false)
            this.getOnePlaylist()
    },
})